<template>
	<view class="page page-fill">
		
		<view class="pending-wapper">
			<image id="face" :src="tempFilePath" class="pending-face" mode="scaleToFill"></image>
		</view>
		
		<view class="notice">
			<view class="notice-words">
				* 请从相册中选择等比宽高的图片噢~
			</view>
		</view>
		
		<view class="footer-opertor">
			<view class="opertor-words" @click="changePendingFace">
				重新选择
			</view>
			<view class="opertor-words" @click="upload">
				确认上传
			</view>
		</view>
		
	</view>
</template>

<script>
	export default {
		data() {
			return {
				tempFilePath:''
			}
		},
		onLoad(params) {
			this.tempFilePath = params.tempFilePath;
		},
		methods: {
			//重新选择
			changePendingFace(){
				uni.chooseImage({
				    count: 1, //默认9
				    sizeType: ['compressed'], //可以指定是原图还是压缩图，默认二者都有
				    sourceType: ['album'], //从相册选择
				    success:  (res)=> {							  
						let tempFilePath = res.tempFilePaths[0];
						this.tempFilePath = tempFilePath;
				    }
				});			
			},
			//确认上传图片
			upload(){
				var globalUser =  uni.getStorageSync("globalUser");
				uni.showLoading({
					mask: true,
					title: "上传中，请稍后"
				});
				
				uni.uploadFile({
					url: this.serverUrl + "/user/uploadFace" + this.user_authority + "&userId=" + globalUser.id,
					filePath: this.tempFilePath,
					name: "file",
					header: {
						"headerUserId": globalUser.id,
						"headerUserToken": globalUser.userUniqueToken
					},
					success(res) {
						var resDataStr = res.data;
						
						var resData = JSON.parse(resDataStr);
						if (resData.status == 200) {
							// 获得最新的用户数据
							var userInfo = resData.data;
							uni.setStorageSync("globalUser", userInfo);
							uni.navigateBack({delta: 1})
						} else if (resData.status == 502 || resData.status == 500) {
							uni.showToast({
								title: res.data.msg,
								image: "../../static/icos/error.png",
								duration: 2000
							})
						}
					},
					complete() {
						uni.hideLoading();
					}
				});
			}
		}
	}
</script>

<style>
	@import url("meFace.css");
</style>
